#!/bin/bash

# Upload all route files to production server
echo "Uploading route files to production server..."

# Upload each file individually
echo "Uploading auth.js..."
scp temp-routes/auth.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading users.js..."
scp temp-routes/users.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading menu.js..."
scp temp-routes/menu.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading orders.js..."
scp temp-routes/orders.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading coupons.js..."
scp temp-routes/coupons.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading payments.js..."
scp temp-routes/payments.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "Uploading admin.js..."
scp temp-routes/admin.js root@47.237.10.129:/opt/cumrbull/backend/src/routes/

echo "All route files uploaded successfully!"

# Verify upload
echo "Verifying uploaded files..."
ssh root@47.237.10.129 'ls -la /opt/cumrbull/backend/src/routes/'

# Restart backend service
echo "Restarting backend service..."
ssh root@47.237.10.129 'cd /opt/cumrbull/backend && pkill -f "node src/app.js" && nohup node src/app.js > /var/log/backend.log 2>&1 &'

# Check service status
echo "Checking service status..."
ssh root@47.237.10.129 'sleep 3 && ps aux | grep "node src/app.js" | grep -v grep'
ssh root@47.237.10.129 'netstat -tlnp | grep :3000'

# Show recent logs
echo "Recent backend logs:"
ssh root@47.237.10.129 'tail -20 /var/log/backend.log'

echo "Route files deployment completed!"